package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starter.home.dao.GroupDao;
import com.starter.home.dao.UserDao;
import com.starter.home.domain.entity.Friends;
import com.starter.home.dao.FriendsDao;
import com.starter.home.domain.entity.Group;
import com.starter.home.domain.entity.User;
import com.starter.home.domain.vo.FriendVO;
import com.starter.home.service.IFriendsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-04
 */
@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsDao, Friends> implements IFriendsService {

    @Autowired
    private GroupDao groupDao;
    @Autowired
    private UserDao userDao;
    @Override
    public PageUtil getFriends(CommonParam commonParam) {
        Page<FriendVO> page = new Page<>(commonParam.getPage(),commonParam.getSize());
        Page<FriendVO> page1 = this.baseMapper.selectAll(page,commonParam);
        List<FriendVO> list = page1.getRecords();
        Group group = null;
        for (FriendVO friendVO : list) {
            if (friendVO.getGroupId() == null){
                continue;
            }
            group = groupDao.selectById(friendVO.getGroupId());
            friendVO.setGroupName(group.getGroupName());
        }
        return new PageUtil(page1.getTotal(), page1.getCurrent(),list);
    }

    @Override
    public int saveRemark(Integer id, Friends friends) {
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_id",id).eq("to_id",friends.getToId());
        Friends friends1 = baseMapper.selectOne(queryWrapper);
        friends1.setRemark(friends.getRemark());
        int i = baseMapper.updateById(friends1);
        return i;
    }

    @Override
    public boolean changStatus(Integer id, Integer status) {
        Friends friends = baseMapper.selectById(id);
        friends.setStatus(status);
        int i = this.baseMapper.updateById(friends);
        return i > 0 ? true : false;
    }

    @Override
    public boolean deleteFriend(Integer id, Integer status) {
        int i = baseMapper.deleteById(id);
        return i > 0 ? true:false;
    }

    @Override
    public List<FriendVO> getFriendsApply(Integer id) {
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_id",id);
        queryWrapper.eq("status",1);
        List<Friends> friends = baseMapper.selectList(queryWrapper);
        List<FriendVO> list = new ArrayList<>();
        FriendVO friendVO = null;
        for (Friends friend : friends) {
            friendVO = new FriendVO();
            BeanUtils.copyProperties(friend,friendVO);
            Integer fromId = friend.getFromId();
            User user = userDao.selectById(fromId);
            user.setPassword("");
            friendVO.setUser(user);
            list.add(friendVO);
        }
        return list;
    }

}
